<template>
    <div>
        <Filters block :form="searchForm" :filterData="filterData" @confirm="changePage(1)" />
        <div class="container">
            <DataTable rowKey="id" :showConfig="false" :tableData="Data" :tableColumn="tableColumn" :pageConfig="pageConfig"></DataTable>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Watch, Vue } from 'vue-property-decorator'
@Component
export default class unitConfig extends Vue {
    ratio:any = 0
    code:any = ''
    expandedKeys: any[] = []
    tree_id:any = ''
    timer:any = null
    Data: any[] = []
    coeffList:any[] = [{
        label: '全部',
        value: 0
    },
    {
        label: '当前系数为1',
        value: 1
    }, {
        label: '当前系数不为1',
        value: 2
    }]
    searchForm: any = {
        searchValue: '',
        ratio: 0
    };
    get filterData () {
        return {
            searchValue: {
                label: '关键词',
                type: 'input',
                template: {
                    placeholder: '搜索物料编码/名称',
                    clearable: true
                }
            },
            ratio: {
                label: '筛选',
                type: 'select',
                width: 200,
                template: {
                    placeholder: '请选择',
                    clearable: true,
                    filterable: true,
                    options: this.coeffList
                }
            }
        };
    }
    autoExpandParent: boolean = true
    replaceFields: any = {
        children: 'child',
        title: 'codeName',
        key: 'id'
    }
    tableColumn: any = {
        code: {
            label: '物料编码',
            width: 150
        },
        name: {
            label: '物料名称',
            width: 300
        },
        assetFlag: {
            label: '是否统计数据',
            width: 100,
            type: 'switch2',
            callback: this.changeSwitch
        },
        measdocName: {
            label: '物料单位',
            width: 150
        },
        settingUnit: {
            label: '标准计量单位',
            width: 150
        },
        convertRatio: {
            label: '单位转换系数',
            width: 150
        },
        changeUnit: {
            label: '编辑转换系数',
            width: 150,
            type: 'input',
            placeholder: '请输入',
            callback: this.handlechange
        },
        action: {
            label: '操作',
            template: [{
                    tag: 'a',
                    text: '移除',
                    callback: this.handleCancel
                }
            ]
        }
    }
    pageConfig: any = {
        current: 1,
        total: 10,
        pageSize: 10,
        onChange: this.changePage
    };
    init () {
        this.getList()
    }
    // 获取列表数据
    getList () {
        let data = {
            typeId: this.$route.query.id,
            page: this.pageConfig.current,
            size: this.pageConfig.pageSize,
            searchValue: this.searchForm.searchValue,
            code: '',
            ratio: this.searchForm.ratio
        }
        this.$api.assetSettingNcMaterielFetch(data).then(res => {
            if (res.code == 0) {
                this.pageConfig.total = res.data.total
                res.data.records.map((item:any) => {
                    if (item.assetFlag == 'Y') {
                        item.assetFlag = true
                    } else {
                        item.assetFlag = false
                    }
                })
                this.Data = res.data.records
            }
        }).catch()
    }
    changeSwitch (val:any, index:any) {
        console.log(val)
        let obj = Object.assign({}, val)
        if (obj.assetFlag) {
            obj.assetFlag = 'Y'
        } else {
            obj.assetFlag = 'N'
        }
        let data = {
            ncMaterielList: [
                {
                    ncMaterielId: val.id,
                    assetFlag: obj.assetFlag
                }
            ]
        }
        this.$api.assetSettingTurnAssetFlagFetch(data).then(res => {
            if (res.code == 0) {
                this.$message.success('操作成功');
                this.getList()
            }
        }).catch()
    }
    handleCancel (val:any) {
        let that = this
        this.$confirm({
        title: '是否确定删除',
        onOk () {
            that.$api.assetSettingDeleteMaterialFetch({materialId: val.id}).then(res => {
            if (res.code == 0) {
                that.$message.success('操作成功');
                that.getList()
            }
        }).catch()
        }
      });
    }
    // 修改系数
    handlechange (val:any) {
        let that = this
        if (val.changeUnit != '') {
            let obj = {
                materialId: val.id,
                convertRatio: val.changeUnit
            }
            // 防抖
            clearTimeout(this.timer)
            that.timer = setTimeout(() => {
                that.$api.assetSettingUpdateConvertRatioFetch(obj).then(res => {
                    if (res.code == 0) {
                        this.$message.success('操作成功');
                        that.getList()
                    }
                }).catch()
            }, 500);
        }
    }
    changePage (val: number) {
        this.pageConfig.current = val;
        if (val == 1) {
            this.getList()
        } else {
            this.getList()
        }
    }
}
</script>
<style lang="scss" scoped>


</style>
